home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / java / io / PipedReader.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  2.5 KB  |  219 lines

  1. package java.io;
  2.  
  3. public class PipedReader extends Reader {
  4.    boolean closedByWriter;
  5.    boolean closedByReader;
  6.    boolean connected;
  7.    Thread readSide;
  8.    Thread writeSide;
  9.    private static final int DEFAULT_PIPE_SIZE = 1024;
  10.    char[] buffer;
  11.    // $FF: renamed from: in int
  12.    int field_0;
  13.    int out;
  14.  
  15.    public PipedReader(PipedWriter var1) throws IOException {
  16.       this(var1, 1024);
  17.    }
  18.  
  19.    public PipedReader(PipedWriter var1, int var2) throws IOException {
  20.       this.closedByWriter = false;
  21.       this.closedByReader = false;
  22.       this.connected = false;
  23.       this.field_0 = -1;
  24.       this.out = 0;
  25.       this.initPipe(var2);
  26.       this.connect(var1);
  27.    }
  28.  
  29.    public PipedReader() {
  30.       this.closedByWriter = false;
  31.       this.closedByReader = false;
  32.       this.connected = false;
  33.       this.field_0 = -1;
  34.       this.out = 0;
  35.       this.initPipe(1024);
  36.    }
  37.  
  38.    public PipedReader(int var1) {
  39.       this.closedByWriter = false;
  40.       this.closedByReader = false;
  41.       this.connected = false;
  42.       this.field_0 = -1;
  43.       this.out = 0;
  44.       this.initPipe(var1);
  45.    }
  46.  
  47.    private void initPipe(int var1) {
  48.       if (var1 <= 0) {
  49.          throw new IllegalArgumentException("Pipe size <= 0");
  50.       } else {
  51.          this.buffer = new char[var1];
  52.       }
  53.    }
  54.  
  55.    public void connect(PipedWriter var1) throws IOException {
  56.       var1.connect(this);
  57.    }
  58.  
  59.    synchronized void receive(int var1) throws IOException {
  60.       if (!this.connected) {
  61.          throw new IOException("Pipe not connected");
  62.       } else if (!this.closedByWriter && !this.closedByReader) {
  63.          if (this.readSide != null && !this.readSide.isAlive()) {
  64.             throw new IOException("Read end dead");
  65.          } else {
  66.             this.writeSide = Thread.currentThread();
  67.  
  68.             while(this.field_0 == this.out) {
  69.                if (this.readSide != null && !this.readSide.isAlive()) {
  70.                   throw new IOException("Pipe broken");
  71.                }
  72.  
  73.                this.notifyAll();
  74.  
  75.                try {
  76.                   this.wait(1000L);
  77.                } catch (InterruptedException var3) {
  78.                   throw new InterruptedIOException();
  79.                }
  80.             }
  81.  
  82.             if (this.field_0 < 0) {
  83.                this.field_0 = 0;
  84.                this.out = 0;
  85.             }
  86.  
  87.             this.buffer[this.field_0++] = (char)var1;
  88.             if (this.field_0 >= this.buffer.length) {
  89.                this.field_0 = 0;
  90.             }
  91.  
  92.          }
  93.       } else {
  94.          throw new IOException("Pipe closed");
  95.       }
  96.    }
  97.  
  98.    synchronized void receive(char[] var1, int var2, int var3) throws IOException {
  99.       while(true) {
  100.          --var3;
  101.          if (var3 < 0) {
  102.             return;
  103.          }
  104.  
  105.          this.receive(var1[var2++]);
  106.       }
  107.    }
  108.  
  109.    synchronized void receivedLast() {
  110.       this.closedByWriter = true;
  111.       this.notifyAll();
  112.    }
  113.  
  114.    public synchronized int read() throws IOException {
  115.       if (!this.connected) {
  116.          throw new IOException("Pipe not connected");
  117.       } else if (this.closedByReader) {
  118.          throw new IOException("Pipe closed");
  119.       } else if (this.writeSide != null && !this.writeSide.isAlive() && !this.closedByWriter && this.field_0 < 0) {
  120.          throw new IOException("Write end dead");
  121.       } else {
  122.          this.readSide = Thread.currentThread();
  123.          int var1 = 2;
  124.  
  125.          while(this.field_0 < 0) {
  126.             if (this.closedByWriter) {
  127.                return -1;
  128.             }
  129.  
  130.             if (this.writeSide != null && !this.writeSide.isAlive()) {
  131.                --var1;
  132.                if (var1 < 0) {
  133.                   throw new IOException("Pipe broken");
  134.                }
  135.             }
  136.  
  137.             this.notifyAll();
  138.  
  139.             try {
  140.                this.wait(1000L);
  141.             } catch (InterruptedException var3) {
  142.                throw new InterruptedIOException();
  143.             }
  144.          }
  145.  
  146.          char var2 = this.buffer[this.out++];
  147.          if (this.out >= this.buffer.length) {
  148.             this.out = 0;
  149.          }
  150.  
  151.          if (this.field_0 == this.out) {
  152.             this.field_0 = -1;
  153.          }
  154.  
  155.          return var2;
  156.       }
  157.    }
  158.  
  159.    public synchronized int read(char[] var1, int var2, int var3) throws IOException {
  160.       if (!this.connected) {
  161.          throw new IOException("Pipe not connected");
  162.       } else if (this.closedByReader) {
  163.          throw new IOException("Pipe closed");
  164.       } else if (this.writeSide != null && !this.writeSide.isAlive() && !this.closedByWriter && this.field_0 < 0) {
  165.          throw new IOException("Write end dead");
  166.       } else if (var2 >= 0 && var2 <= var1.length && var3 >= 0 && var2 + var3 <= var1.length && var2 + var3 >= 0) {
  167.          if (var3 == 0) {
  168.             return 0;
  169.          } else {
  170.             int var4 = this.read();
  171.             if (var4 < 0) {
  172.                return -1;
  173.             } else {
  174.                var1[var2] = (char)var4;
  175.                int var5 = 1;
  176.  
  177.                while(this.field_0 >= 0) {
  178.                   --var3;
  179.                   if (var3 <= 0) {
  180.                      break;
  181.                   }
  182.  
  183.                   var1[var2 + var5] = this.buffer[this.out++];
  184.                   ++var5;
  185.                   if (this.out >= this.buffer.length) {
  186.                      this.out = 0;
  187.                   }
  188.  
  189.                   if (this.field_0 == this.out) {
  190.                      this.field_0 = -1;
  191.                   }
  192.                }
  193.  
  194.                return var5;
  195.             }
  196.          }
  197.       } else {
  198.          throw new IndexOutOfBoundsException();
  199.       }
  200.    }
  201.  
  202.    public synchronized boolean ready() throws IOException {
  203.       if (!this.connected) {
  204.          throw new IOException("Pipe not connected");
  205.       } else if (this.closedByReader) {
  206.          throw new IOException("Pipe closed");
  207.       } else if (this.writeSide != null && !this.writeSide.isAlive() && !this.closedByWriter && this.field_0 < 0) {
  208.          throw new IOException("Write end dead");
  209.       } else {
  210.          return this.field_0 >= 0;
  211.       }
  212.    }
  213.  
  214.    public void close() throws IOException {
  215.       this.field_0 = -1;
  216.       this.closedByReader = true;
  217.    }
  218. }
  219.